 package server.console.groups.setting;





import com.alee.extended.panel.GroupPanel;
import com.alee.extended.panel.GroupingType;
import com.alee.laf.button.WebButton;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.table.WebTable;
import com.alee.laf.text.WebTextField;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import configs.Config;
import configs.ServerConfig;
import handling.channel.ChannelServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.life.MapleLifeFactory;
import server.life.MapleNPC;
import server.maps.MapleMap;
import server.maps.MapleMapFactory;
import tools.JsonUtil;

import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.io.IOException;
import java.util.*;
import java.util.List;




























 public class NpcConfigGroup extends AbstractConfigGroup {
/*  32 */   private static final Logger log = LogManager.getLogger(NpcConfigGroup.class);


/*  35 */   private static final Map<Integer, List<Integer>> data = MapleMapFactory.getAllLinkNpc();
/*  36 */   private final NpcTableMode tableModel = new NpcTableMode();
/*  37 */   private final WebTable webTable = new WebTable(this.tableModel);
   private WebTextField searchText;
   private WebButton searchButton;
   private Map<Integer, List<Integer>> changelist;

   NpcConfigGroup(ConfigPanel owner) {
/*  43 */     super(owner, "NPC相关");

/*  45 */     if (!ServerConfig.WORLD_HIDENPCS.isEmpty()) {
       try {
/*  47 */         this.changelist = (Map<Integer, List<Integer>>)JsonUtil.getMapperInstance().readValue(ServerConfig.WORLD_HIDENPCS, new TypeReference<Map<Integer, List<Integer>>>() {  }
           );
/*  49 */       } catch (IOException e) {
/*  50 */         e.printStackTrace();
       }
     } else {
/*  53 */       this.changelist = new HashMap<>();
     }
   }



   public Component getPreview() {
/*  60 */     this.webTable.setAutoResizeMode(1);
/*  61 */     this.webTable.setGridColor(Color.LIGHT_GRAY);
/*  62 */     this.webTable.getColumnModel().getColumn(2).setMaxWidth(50);
/*  63 */     this.tableModel.addTableModelListener(e -> {
           if (e.getColumn() == 2) {
             int mapid = Integer.parseInt(this.tableModel.getValueAt(e.getFirstRow(), 0).toString().split("\\(")[0]);

             int npcid = Integer.parseInt(this.tableModel.getValueAt(e.getFirstRow(), 1).toString().split("\\(")[0]);

             changeSetting(mapid, npcid, Boolean.valueOf(this.tableModel.getValueAt(e.getFirstRow(), e.getColumn()).toString()).booleanValue());
           }
         });

/*  73 */     AbstractConfigGroup.TitleWebPanel titleWebPanel1 = new AbstractConfigGroup.TitleWebPanel("设置隐藏NPC");

/*  75 */     WebPanel panel = new WebPanel()
       {
























                    private Vector<Vector<Object>> getSearchResult(String id) {
                        Vector<Vector<Object>> ret = new Vector<>();

                        if (data.containsKey(Integer.valueOf(id))) {
                            data.get(Integer.valueOf(id)).forEach(integer -> ret.add(getVectorData(Integer.valueOf(id), integer)));
                        } else {
                            data.entrySet().parallelStream()
                                    .filter(integerListEntry -> integerListEntry.getValue().contains(Integer.valueOf(id)))
                                    .forEach(integerListEntry -> integerListEntry.getValue().parallelStream()
                                            .filter(integer -> integer.equals(Integer.valueOf(id)))
                                            .forEach(integer -> {
                                                ret.add(getVectorData(integerListEntry.getKey(), integer));
                                            }));
                        }

                        return ret;
                    }

         private Vector<Object> getVectorData(int mapid, int npcid) {
/* 121 */           Vector<Object> vector = new Vector();
/* 122 */           vector.add(mapid + "(" + MapleMapFactory.getMapName(mapid) + ")");
/* 123 */           vector.add(npcid + "(" + MapleLifeFactory.getNpcName(npcid) + ")");
/* 124 */           vector.add(Boolean.valueOf(isHide(mapid, npcid)));
/* 125 */           return vector;
         }

         private boolean isHide(int mapid, int npcid) {
/* 129 */           return (NpcConfigGroup.this.changelist.containsKey(Integer.valueOf(mapid)) && ((List)NpcConfigGroup.this.changelist.get(Integer.valueOf(mapid))).contains(Integer.valueOf(npcid)));
         }
       };

/* 133 */     return (Component)new GroupPanel(GroupingType.fillLast, 5, false, new Component[] { (Component)titleWebPanel1, (Component)panel });
   }


private void changeSetting(int mapid, int npcid, boolean isSelected) {

    List<Integer> integers = changelist.computeIfAbsent(mapid, integer -> new ArrayList<>());
    if (!isSelected) {
        integers.remove(Integer.valueOf(npcid));
    } else {
        integers.add(npcid);
    }

    if (integers.isEmpty()) {
        changelist.remove(mapid);
    }


    try {
        ServerConfig.WORLD_HIDENPCS = JsonUtil.getMapperInstance().writeValueAsString(changelist);
        Config.setProperty("world.hidenpcs", ServerConfig.WORLD_HIDENPCS);
    } catch (JsonProcessingException e) {
        log.error("写入配置项失败: world.hidenpcs", e);
    }

    if (!isSelected) {
        ChannelServer.getAllInstances().parallelStream().forEach(c -> {
            MapleMap map = c.getMapFactory().getMap(mapid);
            if (map == null) {
                return;
            }
            MapleNPC npc = map.getNPCById(npcid);
            if (npc == null) {
                return;
            }
            c.getPlayerStorage().getAllCharacters().parallelStream().forEach(p -> npc.sendDestroyData(p.getClient()));
            c.getPlayerStorage().getAllCharacters().parallelStream().forEach(p -> npc.sendSpawnData(p.getClient()));

        });
    } else {

        ChannelServer.getAllInstances().parallelStream().forEach(c -> {

            MapleMap map = c.getMapFactory().getMap(mapid);
            if (map == null) {
                return;
            }
            MapleNPC npc = map.getNPCById(npcid);
            if (npc == null) {
                return;
            }
            c.getMapFactory().getMap(mapid).hideNpc(npcid);
        });
    }
}

   public class NpcTableMode
     extends AbstractTableModel
   {
/* 189 */     public final Object[] longValues = new Object[] { "地图ID(名称)", "NPCID(名称)", Boolean.TRUE };
/* 190 */     private final String[] columnNames = new String[] { "地图ID(名称)", "NPCID(名称)", "隐藏" };
/* 191 */     private Vector data = new Vector();


     public int getColumnCount() {
/* 195 */       return this.columnNames.length;
     }


     public int getRowCount() {
/* 200 */       return this.data.size();
     }


     public String getColumnName(int col) {
/* 205 */       return this.columnNames[col];
     }


@Override
public Object getValueAt(int row, int col) {
    Vector rowVector = (Vector) data.elementAt(row);
    return rowVector.elementAt(col);
}


     public Class getColumnClass(int c) {
/* 216 */       return this.longValues[c].getClass();
     }


     public boolean isCellEditable(int row, int col) {
/* 221 */       return (col >= 2);
     }



    @Override
    public void setValueAt(Object value, int row, int col) {
        Vector rowVector = (Vector) data.elementAt(row);
        rowVector.setElementAt(value, col);
        fireTableCellUpdated(row, col);
    }

     public void setDataVector(Vector data) {
/* 232 */       this.data = nonNullVector(data);
/* 233 */       justifyRows(0, getRowCount());
/* 234 */       fireTableDataChanged();
     }

     private Vector nonNullVector(Vector v) {
/* 238 */       return (v != null) ? v : new Vector();
     }





     private void justifyRows(int from, int to) {
/* 246 */       this.data.setSize(getRowCount());

/* 248 */       for (int i = from; i < to; i++) {
/* 249 */         if (this.data.elementAt(i) == null) {
/* 250 */           this.data.setElementAt(new Vector(), i);
         }
/* 252 */         ((Vector)this.data.elementAt(i)).setSize(getColumnCount());
       }
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\server\console\groups\setting\NpcConfigGroup.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */